package com.patrol.logisticsmanage.ui.mine;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.alibaba.android.arouter.launcher.ARouter;
import com.patrol.logisticsmanage.R;
import com.patrol.logisticsmanage.R2;
import com.patrol.logisticsmanage.net.AppConfig;
import com.patrol.logisticsmanage.ui.canteen.utils.ToastUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import cn.jpush.android.api.JPushInterface;

public class MineFragment extends Fragment {

    @BindView(R2.id.fl_login)
    FrameLayout flLogin;
    @BindView(R2.id.tv_account)
    TextView tvAccount;
    @BindView(R2.id.tv_logout)
    TextView tvLogout;
    @BindView(R2.id.tv_my_order)
    TextView tvMyOrder;
    @BindView(R.id.tv_num)
    TextView tvNum;
    private MineViewModel mineViewModel;
    Unbinder bind;
    boolean isLogin;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
//        mineViewModel =
//                ViewModelProviders.of(this).get(MineViewModel.class);
        View root = inflater.inflate(R.layout.fragment_mine, container, false);

        bind = ButterKnife.bind(this, root);

        isLogin = !TextUtils.isEmpty(AppConfig.getString("token"));
        tvNum.setText("当前版本："+getVersionCode());

        return root;
    }

    private String getVersionCode() {
        // 包管理器 可以获取清单文件信息
        PackageManager packageManager = getContext().getPackageManager();
        try {
            // 获取包信息
            // 参1 包名 参2 获取额外信息的flag 不需要的话 写0
            PackageInfo packageInfo = packageManager.getPackageInfo(
                    getContext().getPackageName(), 0);
            return packageInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return "1.0";
    }

    @Override
    public void onResume() {
        super.onResume();
        isLogin = !TextUtils.isEmpty(AppConfig.getString("token"));
        if (isLogin) {
            tvAccount.setText(AppConfig.getString("realname"));
        }
    }

    @OnClick(R2.id.fl_login)
    public void onViewClicked() {
        if (isLogin) {
            ToastUtils.showToast("当前已登录");
        } else {
            ARouter.getInstance().build("/app/login").navigation();
        }
    }

    @Override
    public void onDestroyView() {
        bind.unbind();
        super.onDestroyView();
    }

    @OnClick(R2.id.tv_logout)
    public void onViewClicked1() {
        isLogin = false;
        tvAccount.setText("暂未登陆");
        AppConfig.putString("token", "");
        SharedPreferences preferences = getContext().getSharedPreferences("info", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.remove("password");
        editor.commit();
//        JPushInterface.deleteAlias(getContext(), 2011);
        ARouter.getInstance().build("/app/login").navigation();
        getActivity().finish();
    }

    @OnClick(R2.id.tv_my_order)
    public void onOrderClicked() {
        ARouter.getInstance().build("/app/myOrders").navigation();
    }

//    @OnClick(R.id.tuijian)
//    public void onTuijianClicked(){
//        ARouter.getInstance().build("/app/tuijian").navigation();
//    }
}